﻿@using iTextSharp.text;
@using iTextSharp.text.pdf;
@using iTextSharp.text.html;
@using iTextSharp.text.xml;
@using System.Collections.Generic;
@using System.Collections;
@using System.Text;

@{
   
   Layout = "~/_SiteLayout.cshtml";

    var custid = UtilityManager.GetVal(Request["custId"]);
     var ccno = UtilityManager.GetVal(Request["ccno"]);
     var fc = UtilityManager.GetVal(Request["fieldChanged"]);
     var changeBy = UtilityManager.GetVal(Request["changedBy"]);
     var toc = UtilityManager.GetVal(Request["toc"]);
     var ov = UtilityManager.GetVal(Request["oldValue"]);
     var nv = UtilityManager.GetVal(Request["newValue"]);
     var dateFrom = UtilityManager.GetDateVal(Request["dateFrom"]);
     var dateTo = UtilityManager.GetDateVal(Request["dateTo"]);
   
    
    
    //var query = "SELECT * FROM AuditTrail WHERE DateCreated >= @0 AND DateCreated <= @1";

     var query = "SELECT * FROM AuditTrail ";
    StringBuilder sb = new StringBuilder();
    sb.Append(query);
    
    //check params
    int pCtr=0;
    Dictionary<string,dynamic> p = new Dictionary<string,dynamic>();
    if(custid!=""){
        p.Add("CustomerID",custid);
        pCtr++;
    }
    if(ccno!=""){
        p.Add("CashCardNo",ccno);
        pCtr++;
    }
    if(fc!=""){
        p.Add("FieldChange",fc);
        pCtr++;
    }
    if(changeBy!=""){
        p.Add("ChangeBy",changeBy);
        pCtr++;
    }
    if(toc!=""){
        p.Add("TypeOfChange",toc);
        pCtr++;
    }
    if(ov!=""){
        p.Add("OldValue",ov);
        pCtr++;
    }
    if(nv!=""){
        p.Add("NewValue",nv);
        pCtr++;
    }
    
    if(dateFrom!=null){
        p.Add("DateFrom",dateFrom);
        pCtr++;
    }
    
   /* if(dateTo!=null){
        p.Add("DateTo",dateTo);
        pCtr++;
    }*/
    
    if(pCtr>0){
        int ctr=0;
        string cond=string.Empty;
        foreach(KeyValuePair<string,dynamic> pair in p){
            if(ctr==0){
                cond =" WHERE";
            }else{
                cond=" AND";
            }
            if((string.Compare(pair.Key,"FieldChange")==0) || (string.Compare(pair.Key,"ChangeBy")==0) || (string.Compare(pair.Key,"TypeOfChange")==0)|| (string.Compare(pair.Key,"OldValue")==0) || (string.Compare(pair.Key,"NewValue")==0)){
                     sb.Append(cond).Append(string.Format(" {0} LIKE '{1}%'",pair.Key,pair.Value));
            }else if(string.Compare(pair.Key,"DateFrom")==0){
                  var newDF = Convert.ToDateTime(dateFrom);
                 var newDT = Convert.ToDateTime(dateTo);
              
                 sb.Append(cond).Append(string.Format(" DateCreated>='{0}' AND DateCreated<='{1}'",newDF,newDT.AddDays(1)));
            }else{
                sb.Append(cond).Append(string.Format(" {0}='{1}'",pair.Key,pair.Value));
            }
             ctr++;   
        }
    }
    
    
    var cols = new[]{"FieldChange","TypeOfChange","OldValue","NewValue","ChangeBy"};
    
    
    var reportData = DBManager.Query(sb.ToString(),null);
   

    DateTime today = DateTime.Today;
    //string ctr = UtilityManager.GetCounterForFileExt();
    string fileName=string.Empty;
     
    fileName = string.Format("AuditReport_{0}.xls",today); 
    string path = "~/Content/Templates/auditreport_template.xls";
    byte[] byteArray= UtilityManager.GenerateAuditReportXL(Server.MapPath(path),"AuditReport",reportData);
        
    
   
    //clear all content output from the buffer stream
    
    Response.Clear();
    //add HTTP Header to the output stream and speficy default name for the browser download dialog
    
    Response.AddHeader("Content-Disposition","attachment; filename="+fileName);
    //add header for file size
    Response.AddHeader("Content-Length",byteArray.Length.ToString());
    //set HTTP Mime Type
    Response.ContentType="application/vnd.ms-excel";
    //Write data to the client
    Response.BinaryWrite(byteArray);
    Response.Flush();
    Response.End();
    
}

